<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        /* 1 */
        function q1() {
            async function async1() {
                console.log('async1 start')
                await async2()
                console.log('async1 end')
            }
            async function async2() {
                console.log('async2')
            }

            console.log('script start')
            setTimeout(function () {
                console.log('setTimeOut')
            }, 0)
            async1()
            new Promise(function (resolve) {
                console.log('promise1')
                resolve()
            }).then(function () {
                console.log('promise2')
            })
            console.log('script end')

            // 改写：
            // function async1() {
            //     return new Promise((resolve, reject) => {
            //         console.log('async1 start')
            //         async2()
            //         resolve()
            //     }).then(() => {
            //         console.log('async1 end')
            //     })
            // }

            // function async2() {
            //     return new Promise((resolve, reject) => {
            //         console.log('async2')
            //         resolve()
            //     })
            // }

            // 输出顺序：
            // script start
            // async1 start
            // async2
            // promise1
            // script end
            // async1 end
            // promise2
            // setTimeOut
        }

        /* 2 */
        function q2() {
            console.log('script start')
            let promise1 = new Promise(function (resolve) {
                console.log('promise1')
                resolve()
                console.log('promise1 end')
            }).then(function () {
                console.log('promise2')
            })
            setTimeout(function () {
                console.log('settimeout')
            })
            console.log('script end')
        }

        /* 3 */
        function q3() {
            async function async1() {
                console.log('async1 start');
                await async2();
                console.log('async1 end')
            }
            async function async2() {
                console.log('async2')
            }

            console.log('script start');
            async1();
            console.log('script end')
        }

        /* 4 */
        function q4() {
            async function async1() {
                console.log('async1 start'); // 2
                await async2();
                console.log('asnyc1 end'); // 6
            }
            async function async2() {
                console.log('async2'); // 3
            }
            console.log('script start'); // 1
            setTimeout(() => {
                console.log('setTimeOut'); // 7
            }, 0);
            async1();
            new Promise(function (reslove) {
                console.log('promise1'); // 4
                reslove();
            }).then(function () {
                console.log('promise2'); // 6
            })
            console.log('script end'); // 5
        }

        /* 5 */
        function q5() {
            async function async1() {
                console.log('async1 start');
                await async2();
                console.log('async1 end');
            }

            async function async2() {
                console.log('async2 start');
                return new Promise((resolve, reject) => {
                    resolve();
                    console.log('async2 promise');
                })
            }

            console.log('script start');

            setTimeout(function () {
                console.log('setTimeout');
            }, 0);

            async1();

            new Promise(function (resolve) {
                console.log('promise1');
                resolve();

            }).then(function () {
                console.log('promise2');
            }).then(function () {
                console.log('promise3');
            });

            console.log('script end');
        }

        /* 6 */
        function q6() {
            async function A() {
                const a = await Promise.resolve(1);
                const b = await B();
                const c = await C();
                const d = await Promise.reject(1).catch(res => res);
                console.log(a === b); // true
                console.log(a === c); // true
                console.log(a === d); // true
            }
            async function B() {
                return 1;
            }
            function C() { return 1 };
            A();
        }

        /* 7 */
        function q7() {
            function foo() { }
            function bar() { }
            foo.prototype = new bar();
            console.log(foo.prototype.__proto__ === new bar().prototype); // false
        }

        /* 8 */
        function q8() {
            console.log(0);
            new Promise(resolve => {
                resolve(1);
                Promise.resolve().then(res => { console.log(2); });
                console.log(3);
            }).then(t => { console.log(t); });
            console.log(4)
        }

        
        function test(callback) {
            console.log(1);
            // return function(...args) {
                return callback()
            // }
        }
        function callback () {
            console.log(arguments)
    </script>
</body>

</html>